package com.example.gif.service.Impl;

import com.example.gif.service.GenerateGifService;
import com.example.gif.utils.ImageUtils;
import com.example.gif.vo.ImageInfo;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import sun.misc.BASE64Decoder;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.net.URL;
import java.util.ArrayList;

/**
 * @author:YangHaoZhong
 * @time:2024/4/8
 */
@Service
public class GenerateGifImpl implements GenerateGifService {
    @Override
    public ByteArrayResource getImage(ImageInfo imageInfo) throws Exception {
        ArrayList<BufferedImage> bufferedImageArrayList = new ArrayList();
        ByteArrayResource animatedGifEncoder;
        if (!CollectionUtils.isEmpty(imageInfo.getUrls())) {
            //根据URL生成Gif图
            URL url2 = new URL(imageInfo.getUrls().get(0));
            BufferedImage bufferedImageFrist = ImageUtils.getBufferedImage(url2);
            imageInfo.getUrls().forEach((urls) -> {
                try {
                    URL url = new URL(urls);
                    BufferedImage bufferedImage = ImageUtils.getBufferedImage(url);
                    BufferedImage resize = resize(bufferedImage, bufferedImageFrist.getWidth(), bufferedImageFrist.getHeight());
                    bufferedImageArrayList.add(resize);
                } catch (Exception var4) {
                    throw new RuntimeException(var4);
                }
            });
            animatedGifEncoder = ImageUtils.getAnimatedGifEncoder(imageInfo.getDelay(), bufferedImageArrayList, bufferedImageFrist.getWidth(), bufferedImageFrist.getHeight());
            return animatedGifEncoder;
        } else if (!CollectionUtils.isEmpty(imageInfo.getImageByteStream())) {
            //根据字节流生成Gif图
            BufferedImage bufferedImageFrist = ImageUtils.bytes2bufImg(imageInfo.getImageByteStream().get(0));
            imageInfo.getImageByteStream().forEach((imageByteStream) -> {
                try {
                    BufferedImage bufferedImage = ImageUtils.bytes2bufImg(imageByteStream);
                    BufferedImage resize = resize(bufferedImage, bufferedImageFrist.getWidth(), bufferedImageFrist.getHeight());
                    bufferedImageArrayList.add(resize);
                } catch (Exception var4) {
                    throw new RuntimeException(var4);
                }
            });
            animatedGifEncoder = ImageUtils.getAnimatedGifEncoder(imageInfo.getDelay(), bufferedImageArrayList, bufferedImageFrist.getWidth(), bufferedImageFrist.getHeight());
            return animatedGifEncoder;
        } else if (!CollectionUtils.isEmpty(imageInfo.getImageBase64())) {
            //根据Base64生成Gif图
            BASE64Decoder decoder = new BASE64Decoder();
            BufferedImage bufferedImageOne = getBufferedImage(imageInfo.getImageBase64().get(0), decoder);
            imageInfo.getImageBase64().forEach((imageBase64) -> {
                try {
                    BufferedImage bufferedImage = getBufferedImage(imageBase64, decoder);
                    BufferedImage resize = resize(bufferedImage, bufferedImageOne.getWidth(), bufferedImageOne.getHeight());
                    bufferedImageArrayList.add(resize);
                } catch (Exception var6) {
                    throw new RuntimeException(var6);
                }
            });
            animatedGifEncoder = ImageUtils.getAnimatedGifEncoder(imageInfo.getDelay(), bufferedImageArrayList, bufferedImageOne.getWidth(), bufferedImageOne.getHeight());
            return animatedGifEncoder;
        } else {
            return null;
        }
    }

    /**
     * 传入Base64返回 BufferedImage
     * @param ImageBase64
     * @param decoder
     * @return
     * @throws Exception
     */
    private static BufferedImage getBufferedImage(String ImageBase64, BASE64Decoder decoder) throws Exception {
        byte[] bytesOne = decoder.decodeBuffer(ImageBase64);
        ByteArrayInputStream baisOne = new ByteArrayInputStream(bytesOne);
        BufferedImage bufferedImageOne = ImageUtils.bufferedImage(baisOne);
        baisOne.close();
        return bufferedImageOne;
    }

    /**
     * 重新设置图片的宽高
     * @param img
     * @param newW
     * @param newH
     * @return
     */
    public static BufferedImage resize(BufferedImage img, int newW, int newH) {
        Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
        BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = dimg.createGraphics();
        g2d.drawImage(tmp, 0, 0, null);
        g2d.dispose();
        return dimg;
    }
}
